package com.sinog.biz.service.business.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sinog.biz.mapper.havexml.AreaCodeFullMapper;
import com.sinog.biz.mapper.havexml.GovernmentHelpMapper;
import com.sinog.biz.service.business.GovernmentHelpService;
import com.sinog.biz.util.IdGen;
import com.sinog.core.domain.ZfbzXxgl;
import com.sinog.core.util.JsonUtil;
import com.sinog.core.util.MapUtil;
import com.sinog.core.util.PageReturnUtil;
import com.sinog.core.utilbean.Result;
import com.sinog.core.utilbean.ReturnCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @desc 安帮工作资源管理 - 政府救助帮扶信息管理 - 政府救助帮扶信息管理 - GovernmentHelpServiceImpl
 * @author yms
 * @date 2020/2/7
 */
@Service
public class GovernmentHelpServiceImpl extends ServiceImpl<GovernmentHelpMapper,ZfbzXxgl> implements GovernmentHelpService {

    @Resource
    private GovernmentHelpMapper governmentHelpMapper;
    @Resource
    private AreaCodeFullMapper areaCodeFullMapper;

    /**
     * 安帮工作资源管理 - 政府救助帮扶信息管理 - 政府救助帮扶信息管理 - 列表
     * @param pageIndex pageIndex
     * @param pageSize pageSize
     * @param querykey querykey
     * @param departId departId
     * @param level level
     * @return String
     */
    @Override
    public String getGovBfList(Integer pageIndex,Integer pageSize,Map<String,Object> querykey,String departId,String level) {
        MapUtil.requestConversionToMap(querykey);
        Page<Map<String,String>> page = new Page<>(pageIndex + 1,pageSize);
        page = page.setRecords(governmentHelpMapper.getxxblList(page,querykey,departId,level));
        return PageReturnUtil.pageReturn(page);
    }

    /**
     * 安帮工作资源管理 - 政府救助帮扶信息管理 - 政府救助帮扶信息管理 - （修改/新增）
     * @param data data
     * @return Result
     */
    @Override
    public Result saveBaseInfo(String data) {
        //参数实体
        ZfbzXxgl zfbzXxgl = JsonUtil.toBean(data,ZfbzXxgl.class);
        Date date = new Date();
        //新增
        if(StringUtils.isBlank(zfbzXxgl.getId())) {
            zfbzXxgl.setId(IdGen.strid());
            zfbzXxgl.setCreateTime(date);
        }
        zfbzXxgl.setUpdateUser(zfbzXxgl.getCreateUser());
        zfbzXxgl.setUpdateTime(date);
        // 保存住址省、市、县、街道名称

        if (StringUtils.isNotBlank(zfbzXxgl.getBfProvince())){
            Map<String,String> areaNames = areaCodeFullMapper.getAreaNames(zfbzXxgl.getBfProvince(),zfbzXxgl.getBfCity(),zfbzXxgl.getBfCounty(),zfbzXxgl.getBfClab());
            if (areaNames == null){
                return Result.failure(ReturnCode.SAVE_FAILED);
            }
            zfbzXxgl.setBfprovinceName(areaNames.get("shengName"));
            zfbzXxgl.setBfcityName(areaNames.get("dishiName"));
            zfbzXxgl.setBfcountyName(areaNames.get("quxianName"));
            zfbzXxgl.setBfclabName(areaNames.get("jiedaoName"));
            if (StringUtils.isBlank(zfbzXxgl.getBfCity())){
                zfbzXxgl.setBfcityName("");
            }
            if (StringUtils.isBlank(zfbzXxgl.getBfCounty())){
                zfbzXxgl.setBfcountyName("");
            }
            if (StringUtils.isBlank(zfbzXxgl.getBfClab())){
                zfbzXxgl.setBfclabName("");
            }
        } else {
            zfbzXxgl.setBfprovinceName("");
            zfbzXxgl.setBfcityName("");
            zfbzXxgl.setBfcountyName("");
            zfbzXxgl.setBfclabName("");
        }
        //入库
        boolean tag = zfbzXxgl.insertOrUpdate();
        if(tag) {
            return Result.success("保存成功");
        }
        return Result.failure(ReturnCode.SAVE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 政府救助帮扶信息管理 - 政府救助帮扶信息管理 - 删除
     * @param id id
     * @return Result
     */
    @Override
    public Result deleteBfInfo(String id) {
        ZfbzXxgl zfbzXxgl = new ZfbzXxgl();
        boolean delFlag = zfbzXxgl.deleteById(id);
        if(delFlag) {
            return Result.success("删除成功！");
        }
        return Result.failure(ReturnCode.DELETE_FAILED);
    }

    /**
     * 安帮工作资源管理 - 政府救助帮扶信息管理 - 政府救助帮扶信息管理 - 导出
     * @param departId departId
     * @return List<Map < String,Object>>
     */
    @Override
    public List<Map<String,Object>> selectBfInfoForExport(String departId) {
        return governmentHelpMapper.selectBfInfoForExport(departId);
    }

    /**
     * 根据帮扶类目,查询出对应的帮扶项目
     * @param bfId bfId
     * @return List<ZfbzXxgl>
     */
    @Override
    public List<ZfbzXxgl> selectBfList(String bfId) {
        return governmentHelpMapper.selectBfList(bfId);
    }

    /**
     * 根据帮扶救助id,查询出对应的救助金额
     * @param id id
     * @return String
     */
    @Override
    public String selectBfInfo(String id) {
        ZfbzXxgl zfbzXxgl = governmentHelpMapper.selectById(id);
        //补贴形式:现金 补贴金额:1000
        //(x > y) ? x : y;
        String bttype = (StringUtils.isNotBlank(zfbzXxgl.getBttype())) ? zfbzXxgl.getBttype() : "无";
        String cash = (0.0 == zfbzXxgl.getBtje()) ? "无" : Double.valueOf(zfbzXxgl.getBtje()).toString();
        return "补贴形式:" + bttype + ",补贴金额:" + cash;
    }
}